// pages/detail/index.js
const http = require('../../utils/http.js');
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
   id:null,
   title:null,
   content:null,
   imgs:{},
   shipin:null,
   createTime:null,
   name:null,
   fstName:null,
   hasvideo: false,
  },
   //转义方法
   escape2Html(str) {
    var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; }).replace('<section', '<div').replace('<img', '<img style="max-width:100%;height:auto" ');
  },
  async loadnews() {
    //postReq(url, data, cb)
    
    let url = '/news/detail';
    let data = {
      id: this.data.id,
     
    }
    let res= await http.post(url,data)
  
    let imgs=res.news.imgs
    imgs = imgs.split(",")
    
    this.setData({
      title: res.news.title,
      content: this.escape2Html(res.news.content),
      name: res.news.name,
      fstName: res.news.fstName,
      createTime: res.news.createTime,
      shipin:res.news.shipin,
      imgs: imgs,
     

    });
   
    if (this.data.shipin != null){
     
      this.setData({
        hasvideo:true
      })
    }
   

  },//end of loadnews
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgs,
      current: e.currentTarget.dataset.url
    });
  },
  toback:function(){
    //返回
    wx.navigateBack({
      delta: 1
    });

  },// end of toback
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    let id = options.id;
   
    this.setData({
      id: id,
    

    });
    //加载详情
    this.loadnews();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})